<?php

class DataBase
{
    private $host;
    private $user;
    private $pwd;
    private $select;

    function __construct($host, $user, $pwd, $select)
    {
        $this->host = $host;
        $this->user = $user;
        $this->pwd = $pwd;
        $this->select = $select;
    }

    function testConn()
    {
        $conn = mysqli_connect($this->host, $this->user, $this->pwd, $this->select);
        if (!$conn) return false;
        else return true;
    }

    function updateConn()
    {
        $content = file_get_contents('conn.php');
        $content = preg_replace('/conn(\s*)=(.*)/', "conn=mysqli_connect('$this->host','$this->user','$this->pwd','$this->select');", $content);
        file_put_contents('conn.php', $content);
    }

    public function getHost()
    {
        return $this->host;
    }

    public function setHost($host): void
    {
        $this->host = $host;
    }

    public function getUser()
    {
        return $this->user;
    }

    public function setUser($user): void
    {
        $this->user = $user;
    }

    public function getPwd()
    {
        return $this->pwd;
    }

    public function setPwd($pwd): void
    {
        $this->pwd = $pwd;
    }

    public function getSelect()
    {
        return $this->select;
    }

    public function setSelect($select): void
    {
        $this->select = $select;
    }
}

if (isset($_POST['install_db_conn'])) {
    $con = new DataBase($_POST['db_host'], $_POST['db_user'], $_POST['db_pwd'], $_POST['db_select']);
    if ($con->testConn()) echo '1';
    else echo '0';
} elseif (isset($_POST['update_conn'])) {
    $con = new DataBase($_POST['db_host'], $_POST['db_user'], $_POST['db_pwd'], $_POST['db_select']);
    $con->updateConn();
}

?>